package leetcode;

public class babaisisi {
    public static boolean backspaceCompare(String s, String t) {
        // 创建两个字符串数组
        char[] s1=s.toCharArray();
        char[] s2=t.toCharArray();
        // 创建两个StringBuffer
        StringBuffer ret=new StringBuffer();
        StringBuffer ret1=new StringBuffer();
        for(char m:s1){
            if(ret.length()>0&&m=='#'){
                //出栈
//                int p=ret.length();
                ret.deleteCharAt(ret.length()-1);
            }else{
                //入栈
                if(m=='#'){
                    continue;
                }
                ret.append(m);
            }
        }
        for(char n:s2){
            if(ret1.length()>0&&n=='#'){
                //出栈
                ret1.deleteCharAt(ret1.length()-1);
            }else{
                //入栈
                if(n=='#'){
                    continue;
                }
                ret1.append(n);
            }
        }
        return (ret.toString()).equals(ret1.toString());

    }

    public static void main(String[] args) {
        String s = "y#fo##f";
        String t = "y#f#o##f";
        boolean b = backspaceCompare(s, t);
        System.out.println(b);

    }
}
